#！/usr/bin/env  bash
#
# Author     : OWES , 基于 Jack zhao 的脚本修改
# Data         : 2021/3/1
# Description: This script is used to monitor the use of local system resources.
# yum install sysstat
# yum -y install bc


###The global variable###
export PATH=$PATH:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export LANG=C
INITDIR=`pwd`
LOCAL_TIME=`date '+%F %H:%M:%S'`
BEFORE_DAY=`date -d "-1 days" +%F`
WHOLE_TIME=`date '+%H'`

###报警值###
LOAD_AVERAGE_ALARM=2 #15分钟内的平均负载，参考：一般小于机器物理CPU个数
CPU_ALARM=70 #CPU使用率
MEMORY_ALARM=1 #内存使用率
SWAP_ALARM=30 #虚拟内存使用率
ROOT_PART_ALARM=95 #/挂载点硬盘使用率
IN_NETWORK_TRAFFIC_ALARM=167772160 #网卡每分钟接收流量20M
OUT_NETWORK_TRAFFIC_ALARM=167772160 #网络每分钟发送流量20M

####定时推送全部信息，将此脚本在 每个小时的第一分钟 执行 ###
TIMING=08 # 早上8点

###Public function###
out_format(){
    printf "%-22s %-5s %-5s \n" "$1" ":" "$2"
}

###Send DingDingRobotMsg###
send_msg(){
	content="服务器资源提醒：\n ""`cat $INITDIR/Monitoring.log`" 
	echo "$content"
	# Send Ding Msg
	curl "https://oapi.dingtalk.com/robot/send?access_token=94997c0114062295c3b3bd389fb24f3555172c754363c9a02b09d10926a46d3d" \
	-H "Content-Type: application/json" \
	-d "{\"msgtype\": \"text\", \"text\": {\"content\": \"${content}\"}}"
}

###Get load information 15 minutes###
get_load_average(){
    LOAD_AVERAGE=`uptime | awk -F "," '{print$NF}' | sed 's#[[:space:]]##g' `
    out_format "Load average"  "$LOAD_AVERAGE" >> $INITDIR/Monitoring.log
    return 0
}

###Get CPU usage###
get_cpu_usage(){
    CPU_FREE=`vmstat 1 5 |sed -n '3,$p' |awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'`
    CPU_USAGE=$((100 - $CPU_FREE))
    out_format "CPU usage"  "${CPU_USAGE}%" >> $INITDIR/Monitoring.log
    return 0
}

###Get memory usage###
get_memory_usage(){
    MEMORY_USED=`free -m  | grep "^Mem" | awk '{print$3}'`
#`free -m | grep -w "buffers\/cache:" | awk '{print$3}'`
    MEMORY_TOTAL=`free -m  | grep "^Mem" | awk '{print$2}'`
    echo -e "mem"$MEMORY_USED"="$MEMORY_TOTAL 
    MEMORY_USAGE=`echo "scale=2;${MEMORY_USED}/${MEMORY_TOTAL}*100;" | bc -l`
    out_format "Memory usage"  "${MEMORY_USAGE}%" >> $INITDIR/Monitoring.log
    return 0
}

###Get Swap usage###
get_swap_usage(){
    SWAP_USED=`free -m | grep -w "^Swap" | awk '{print$3}'`
    SWAP_TOTAL=`free -m | grep -w "^Swap" | awk '{print$2}'`
    SWAP_USAGE=`echo "scale=2;${SWAP_USED}/${SWAP_TOTAL}*100;" | bc -l`
    out_format "Swap usage"  "${SWAP_USAGE}%" >> $INITDIR/Monitoring.log
    return 0
}

###Get disk usage###
get_disk_usage(){
    MOUNT_POINT=`df -hP | egrep -wv  '^tmpfs|Filesystem|boot' | awk '{print$NF}'`
    for i in `echo $MOUNT_POINT`;do
        DISK_USAGE=`df -hP | grep -w "$i" | awk '{print$5}'`
        out_format "Disk usage $i"  "${DISK_USAGE}" >> $INITDIR/Monitoring.log
    done
    ROOT_PART=`df -hP  | grep -w "/" | awk '{print$5}' | awk -F "%" '{print$1}'`
    return  0
}

###Get network traffic###
get_network_traffic(){
    NETWORK_TRAFFIC=`sar -n DEV 1 60|grep Average|grep ens160|awk '{print "Input:",$5*1000*8,"bps","Output:",$6*1000*8,"bps"}'`
    out_format "Network traffic"  "${NETWORK_TRAFFIC}" >> $INITDIR/Monitoring.log
    return 0
}

###Server information###
IP=$(hostname -I)
#`ip addr show eth0 | grep -w "inet" | awk -F "[ /]+" '{print$3}'`
HOSTNAME=`hostname`
SN=`sudo -b /usr/sbin/dmidecode | grep -A5 "System Information" | grep "Serial Number"| awk -F ":" '{print$2}' | sed 's#^[[:space:]]##g'`
OS=`cat /etc/redhat-release`

###Program entrance###
echo "#### Date:$LOCAL_TIME ####" > $INITDIR/Monitoring.log
out_format "IP"  "$IP" >> $INITDIR/Monitoring.log
out_format "Hostname"  "$HOSTNAME" >> $INITDIR/Monitoring.log
#out_format "SN"  "$SN" >> $INITDIR/Monitoring.log
out_format "Os"  "$OS" >> $INITDIR/Monitoring.log

###Call function###
get_load_average
get_cpu_usage
get_memory_usage
#get_swap_usage
get_disk_usage
#get_network_traffic

###variable format###
LOAD_AVERAGE_FORMAT=`echo $LOAD_AVERAGE | awk -F "." '{print$1}'`
MEMORY_USAGE_FORMAT=`echo $MEMORY_USAGE | awk -F "." '{print$1}'`
SWAP_USAGE_FORMAT=`echo $SWAP_USAGE | awk -F "." '{print$1}'`
IN_NETWORK_TRAFFIC=`echo $NETWORK_TRAFFIC | awk '{print$2}'`
OUT_NETWORK_TRAFFIC=`echo $NETWORK_TRAFFIC | awk '{print$5}'`

###Record history log###
cat $INITDIR/Monitoring.log >> $INITDIR/Monitor_history.log

##Monitoring threshold judgment###
if [[ ${LOAD_AVERAGE_FORMAT} -ge ${LOAD_AVERAGE_ALARM} ]];then
    send_msg "$IP Load:High load average."
elif [[  $CPU_USAGE -ge $CPU_ALARM ]]; then
    send_msg "$IP CPU:usage greater than ${CPU_ALARM}%."
elif [[  $MEMORY_USAGE_FORMAT -ge $MEMORY_ALARM ]];then
    send_msg "$IP Memory:usage greater than ${MEMORY_ALARM}%."
elif [[  $SWAP_USAGE_FORMAT -ge $SWAP_ALARM ]];then
    send_msg "$IP Swap:usage greater than ${SWAP_ALARM}%."
elif [[  $ROOT_PART -ge $ROOT_PART_ALARM ]]; then
    send_msg "$IP Root Partition:usage greater than ${ROOT_PART_ALARM}%."
elif [[  $IN_NETWORK_TRAFFIC -ge $IN_NETWORK_TRAFFIC_ALARM ]]; then
    send_msg "$IP Network Traffic:Input Greater than ${IN_NETWORK_TRAFFIC_ALARM}bps."
elif [[  $OUT_NETWORK_TRAFFIC -ge $OUT_NETWORK_TRAFFIC_ALARM ]]; then
    send_msg "$IP Network Traffic:Output Greater than ${OUT_NETWORK_TRAFFIC_ALARM}bps."
fi

###定时推送全部数据###
if [[ "$WHOLE_TIME" == "$TIMING" ]];then
	send_msg "[Notice]:$IP Monitoring information"
fi


